package com.cqc.jdk8.lambda;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * lambda表达式 代替匿名内部类
 */
public class LambdaTest {

    List<String> names=null;
    @Before
    public void before() {
        names= Arrays.asList("lucy","mike","jack");
    }


    @Test
    public void f2(){
        Collections.sort(names,(String a,String b)->{
            return a.compareTo(b);
        });
        System.out.println(names);
    }

    @Test
    public void f3(){
//        Collections.sort(names,(String a,String b)-> b.compareTo(a));
//        Collections.sort(names,( a, b)-> b.compareTo(a));
//        names.sort(( a, b)-> b.compareTo(a));
        names.sort(String::compareTo);
        System.out.println(names);
    }


    @Test
    public void f1(){
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return a.compareTo(b);
            }
        });
        System.out.println(names);
        Collections.sort(names,(a,b)->{ return a.compareTo(b);
        });
    }


    @Test
    public void f(){
        Comparator<String> comparator=new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        Collections.sort(names, comparator);
        System.out.println(names);
    }
}
